package com.glut.service.impl;

import com.glut.mapper.ExaminationMapper;
import com.glut.mapper.FsAnswerMapper;
import com.glut.mapper.SdAnswerMapper;
import com.glut.service.ExaminationService;
import com.glut.vo.ErrorQuestion;
import com.glut.vo.Examination;
import com.glut.vo.FsAnswers;
import com.glut.vo.SdAnswers;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:shenly
 * @date:2020/05/03
 * @Description:
 */
@Service("examinationService")
public class ExaminationServiceImpl implements ExaminationService{

    @Autowired
    ExaminationMapper examinationMapper;

    @Autowired
    SdAnswerMapper sdAnswerMapper;

    @Autowired
    FsAnswerMapper fsAnswerMapper;

    @Override
    public int addExam(Examination examination) {
        return 0;
    }

    /**
     * 试卷信息存入数据库---exam,sd_answer,fs_answer
     * @param examNo 试卷Id
     * @param userId 生成试卷的用户Id
     * @param paperTotalScore 试卷总分
     * @param paperDifficulty 试卷难度
     * @param qIdList 试卷中的题目Id集合
     * @param qtList 试卷中的题型Id集合List<Map<String, Object> Object为ExamQuestiontype类型
     * @return
     */
   @Override
    public boolean inToDB(Long examNo, String userId, int paperTotalScore, double paperDifficulty, List<Long> qIdList, List<Map<String, Object>> qtList,Integer totalScores,Integer totalValues) {

        Examination exam = null;
        if (exam == null) {
            // 新建试卷，将试卷存入数据库
            exam = new Examination(examNo, userId, new Date(), paperDifficulty,totalScores,totalValues);
            int count = addExam(exam);

            if (count != 1) {
                //throw new BasicException("exam添加失败");
            }

        }
        return false;
    }

    /**
     * 查询测试信息
     * @param examNo
     * @return
     */
    @Override
    public List<SdAnswers> selectSdExamById(@Param("userId")String userId,
                                      @Param("examNo")String examNo){
        return examinationMapper.selectSdExamById(userId,examNo);
    }

    /**
     * 通过userId,examNo,qId查询题信息
     * @param errorQuestion
     * @return
     */
    @Override
    public List<SdAnswers> selectSdErrorById(ErrorQuestion errorQuestion){
        return examinationMapper.selectSdErrorById(errorQuestion);
    }
    /**
     * 查询测试信息
     * @param
     * @return
     */
    @Override
    public List<FsAnswers> selectFsExamById(@Param("userId")String userId,
                                      @Param("examNo")String examNo){
        return examinationMapper.selectFsExamById(userId,examNo);
    }

    /**
     * 查询测试信息
     * @param
     * @return
     */
    @Override
    public List<FsAnswers> selectShExamById(@Param("userId")String userId,
                                            @Param("examNo")String examNo){
        return examinationMapper.selectShExamById(userId,examNo);
    }


    /**
     * 通过userId,examNo,qId查询题信息
     * @param errorQuestion
     * @return
     */
    @Override
    public List<FsAnswers> selectFsErrorById(ErrorQuestion errorQuestion){
        return examinationMapper.selectFsErrorById(errorQuestion);
    }

    /**
     * 通过userId,examNo,qId查询题信息
     * @param errorQuestion
     * @return
     */
    @Override
    public List<FsAnswers> selectShErrorById(ErrorQuestion errorQuestion){
        return examinationMapper.selectShErrorById(errorQuestion);
    }
    /**
     * 查询历史测试信息
     * @param userId
     * @return
     */
    @Override
    public List<Examination> selectAllExam(String userId) {
        return examinationMapper.selectAllExam(userId);
    }

    /**
     * 查询历史测试信息
     * @param userId
     * @return
     */
    @Override
    public List<FsAnswers> selectAllFsExam(String userId) {
        return examinationMapper.selectAllFsExam(userId);
    }

    /**
     * 新增考卷
     * @param map
     */
    @Override
    public void insertExam(Map<String, Object> map) {
        examinationMapper.insertExam(map);
    }

    /**
     * 新增考卷
     * @param map
     */
    @Override
    public void insertFsExam(Map<String,Object> map){
        examinationMapper.insertFsExam(map);
    }

    /**
     * 更新考卷
     * @param
     */
    @Override
    public int updateExamination(Examination examination){
        return examinationMapper.updateExamination(examination);
    }

    /**
     * 更新考卷、主观题分数
     * @param map
     */
    @Override
    public void updateExam(Map<String, Object> map) {
        examinationMapper.updateExam(map);

    }

}
